﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    PIMService pim = new PIMService();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Redirect("Default.aspx");
        if (!IsPostBack)
        {
            if (Request.Browser.IsMobileDevice)
            {
                string sPath = Request.Url.AbsoluteUri;
                string sPage = sPath.Substring(sPath.LastIndexOf("/") + 1);
                var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
                if (sPath.ToLower().Contains("mealplandetails.aspx?") || sPath.ToLower().Contains("newrecipe.aspx?"))
                {
                    Response.Redirect("mobile/" +sPage);
                }else
                    Response.Redirect("mobile/index.aspx");
            }

            if (Request.Cookies["PIMUserName"] != null && Request.Cookies["PIMPassword"] != null)
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "error", "window.onload = loadcookie('" + Request.Cookies["PIMUserName"].Value + "','"
                                        + EncryptDecryptQueryString.Decrypt(Request.Cookies["PIMPassword"].Value) + "');", true);
            }
        }
        if (Session["UserName"] != null)
        {
            li3.Style.Add("display", "inherit");
            li4.Style.Add("display", "inherit");
            li5.Style.Add("display", "inherit");
            li6.Style.Add("display", "inherit");

        }
        else
        {
            li3.Style.Add("display", "none");
            li4.Style.Add("display", "none");
            li5.Style.Add("display", "none");
            li6.Style.Add("display", "none");

        }

    }

    protected void SignIn_Click(object sender, ImageClickEventArgs e)
    {
        Login();
    }

    private void Login()
    {
        string url = string.Empty;
        try
        {
            string result = pim.Login(username.Value, password.Value);
            if (!string.IsNullOrEmpty(result))
            {
                if (cbRemember.Checked)
                {
                    Response.Cookies["PIMUserName"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["PIMPassword"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["PIMUserName"].Value = username.Value.Trim();
                    Response.Cookies["PIMPassword"].Value = EncryptDecryptQueryString.Encrypt(password.Value.Trim());
                }
                else
                {
                    Response.Cookies["PIMUserName"].Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies["PIMPassword"].Expires = DateTime.Now.AddDays(-1);

                }
                url = "AllMealPlans.aspx";
            }
            else
            {
                //ScriptManager.RegisterClientScriptBlock(this.GetType(), this, "", "alert(Person Not found.)", true);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Email or password are wrong.');", true);
            }
        }
        catch (Exception ex)
        {

        }
        if (url.Length > 0)
            Response.Redirect(url);
    }

  
}
